
package com.elong.common.http;

import java.net.URL;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.beans.LinkBean;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.visitors.NodeVisitor;




public class VisitorParser {

    /**
     * @Description: visitor模式parse网页
     * 
     * @throws
     */
    public static void parseByVisitor(){
        try {
            Parser parser = new Parser();
            parser.setURL("http://www.baidu.com");
            parser.setEncoding(parser.getEncoding());
            NodeVisitor visitor = new NodeVisitor() {
                public void visitTag(Tag tag){
                    System.out.println("testVisitorAll() Tag name is:" + tag.getTagName()
                            + "\n Class is:" + tag.getClass());
                }
            };
            parser.visitAllNodesWith(visitor);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * @Description: 通过filter方式访问
     * 
     * @throws
     */
    public static void parseByFilter(){
        try {
            NodeFilter filter = new NodeClassFilter(LinkTag.class);
            Parser parser = new Parser();
            parser.setURL("http://www.baidu.com");
            parser.setEncoding(parser.getEncoding());
            NodeList list = parser.extractAllNodesThatMatch(filter);
            for(int i = 0; i < list.size(); i++){
                LinkTag node = (LinkTag)list.elementAt(i);
                System.out.println("testLinkTag() Link is:" + node.extractLink());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * @Description: 通过beans方式来实现
     * 
     * @throws
     */
    public static void visitByBeans(){
        Parser parser = new Parser();
        LinkBean linkBean = new LinkBean();
        linkBean.setURL("https://secure.elong.com/passport/login_cn.html?nexturl=http%3a%2f%2fmy.elong.com%2findex_cn.html%3frnd%3d20140912174839009#?");
        URL[] urls = linkBean.getLinks();
        for(int i = 0; i < urls.length; i++){
            URL url = urls[i];
            System.out.println("testLinkBean()-url is :" + url);
        }
        try {
            parser.parse(new NodeFilter() {
                
                /**
                
                  * @Fields serialVersionUID : TODO（用一句话描述这个变量表示什么）
                
                  */
                
                
                private static final long serialVersionUID = 1L;

                public boolean accept(Node node) {
                    // TODO Auto-generated method stub
                    
                    return false;
                }
            });
        } catch (ParserException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        VisitorParser.visitByBeans();
    }
}
